package com.EAIPClub.ClassesManager.service.impl;

import com.EAIPClub.ClassesManager.mapper.ClassesDataInfoMapper;
import com.EAIPClub.ClassesManager.service.ClassesDataInfoManagerService;
import com.api.POJO.ClassesManager.ClassesDataInfo;
import com.api.POJO.ClassesManager.ClassesManagerReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Author :
 * @Date : 2021/12/12
 */
@Service
@Slf4j
public class ClassesDataInfoManagerServiceImpl implements ClassesDataInfoManagerService {

    private ClassesDataInfoMapper mapper;

    @Autowired
    public void setMapper(ClassesDataInfoMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public ClassesManagerReturnMsg addNewClassesDataInfo(ClassesDataInfo info) {
        ClassesManagerReturnMsg returnMsg = new ClassesManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将新增课程章节信息.info:{}",info);
        if (info==null){
            log.warn("检测到章节信息为空!");
            returnMsg.setStatus(500);
            Msg.add("检测到章节信息为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        Boolean aBoolean = mapper.addNewClassesDataInfo(info);
        if (!aBoolean){
            log.warn("添加课程章节信息失败!");
            returnMsg.setStatus(500);
            Msg.add("添加课程章节信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("添加课程章节信息成功!info:{}",info);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public ClassesManagerReturnMsg getClassesDataInfoByUid(String uid) {
        ClassesManagerReturnMsg returnMsg = new ClassesManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过uid获取章节信息.uid:{}",uid);
        if (uid ==null || uid.equals("")){
            log.warn("检测到uid为空!");
            returnMsg.setStatus(500);
            Msg.add("获取章节信息失败!uid为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        ClassesDataInfo classesDataInfoByUid = mapper.getClassesDataInfoByUid(uid);
        if (classesDataInfoByUid==null){
            log.warn("未能通过uid查询到章节信息!");
        }
        log.warn("通过uid查找章节信息完毕.uid:{},info:{}",uid,classesDataInfoByUid);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        returnMsg.setDataInfo(classesDataInfoByUid);
        return returnMsg;
    }

    @Override
    public ClassesManagerReturnMsg getClassesDataInfoByClassUid(String classesUid) {
        ClassesManagerReturnMsg returnMsg = new ClassesManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过课程uid获取章节信息.classesuid:{}",classesUid);
        if (classesUid==null || classesUid.equals("")){
            log.warn("检测到课程uid为空!");
            returnMsg.setStatus(500);
            Msg.add("获取章节信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        List<ClassesDataInfo> classesDataInfoByClassesUid = mapper.getClassesDataInfoByClassesUid(classesUid);
        if (classesDataInfoByClassesUid==null){
            log.warn("通过课程uid查询数据库失败,返回为空！");
            returnMsg.setStatus(500);
            Msg.add("查询章节信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("通过课程uid查找章节信息成功.uid:{},result:{}",classesUid,classesDataInfoByClassesUid);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        returnMsg.setDataInfos(classesDataInfoByClassesUid);
        return returnMsg;
    }

    @Override
    public ClassesManagerReturnMsg deleteClassesDataInfoByUid(String uid) {
        ClassesManagerReturnMsg returnMsg = new ClassesManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过uid删除章节信息.uid:{}",uid);
        if (uid==null || uid.equals("")){
            log.warn("检测到uid为空!");
            returnMsg.setStatus(500);
            Msg.add("通过uid删除章节信息失败!uid为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        Boolean aBoolean = mapper.deleteClassesDataInfoByUid(uid);
        if (!aBoolean){
            log.warn("通过uid删除数据库章节信息失败!");
            returnMsg.setStatus(500);
            Msg.add("通过uid删除章节信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("通过uid删除章节信息成功.uid:{}",uid);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }
}
